package com.wenhai.spring.dependency.injection;

import com.wenhai.spring.dependency.injection.annotation.UserGroup;
import com.wenhai.xiaomage.spring.ioc.overview.dependency.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Collection;

/**
 * @Author:wenhai
 * @Description: {@link Qualifier} 演示限定注入
 * @Date:2020/3/10
 * @see Qualifier
 */
public class QualifierAnnotationDependencyInjectionDemo {

    @Autowired
    @Qualifier("user")
    private User qualifierUser;

    @Autowired
    private User user;

    @Autowired
    private Collection<User> allUsers;

    @Autowired
    @Qualifier
    private Collection<User> qualifiedUsers;

    @Autowired
    @UserGroup
    private Collection<User> userGroupUsers;



    public static void main(String[] args) {
        AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext();
        cxt.register(QualifierAnnotationDependencyInjectionDemo.class,ConfigUser.class);
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(cxt);
        String xmlResourcePath = "classpath:/META-INF/dependency-lookup-context.xml";
        beanDefinitionReader.loadBeanDefinitions(xmlResourcePath);
        cxt.refresh();
        QualifierAnnotationDependencyInjectionDemo demo = cxt.getBean(QualifierAnnotationDependencyInjectionDemo.class);
        System.out.println(demo.user);
        System.out.println(demo.qualifierUser);
        System.out.println(demo.allUsers);
        System.out.println(demo.qualifiedUsers);
        System.out.println(demo.userGroupUsers);
        cxt.close();
    }
}
